#!/bin/bash
# This script installs sockperf and runs the regression tests
###SHELLPACK preamble sockperf-bench 0
TASKSET_SERVER=
TASKSET_CLIENT=
SERVER_HOST=127.0.0.1
if [ "$REMOTE_SERVER_HOST" != "" ]; then
	SERVER_HOST=$REMOTE_SERVER_HOST
fi

###SHELLPACK parseargBegin
###SHELLPACK parseargInstall
###SHELLPACK parseargBindings
###SHELLPACK parseargParam    --test-type    SOCKPERF_TESTTYPE
###SHELLPACK parseargParam    --protocol     SOCKPERF_PROTOCOL
###SHELLPACK parseargParam    --duration     SOCKPERF_DURATION
###SHELLPACK parseargParam    --msg-sizes    SOCKPERF_MESSAGE_SIZES
###SHELLPACK parseargParam    --msg-rates    SOCKPERF_MESSAGE_RATES
###SHELLPACK parseargEnd
###SHELLPACK monitor_hooks

###SHELLPACK check_install_required sockperf-${VERSION}
###SHELLPACK init_complete

SOCKPERF_MESSAGE_SIZES=`echo $SOCKPERF_MESSAGE_SIZES | tr ',' ' '`
SOCKPERF_MESSAGE_RATES=`echo $SOCKPERF_MESSAGE_RATES | tr ',' ' '`

if [ "$SOCKPERF_PROTOCOL" = "" ]; then
	SOCKPERF_PROTOCOL=`echo $SERVERSIDE_NAME | awk -F - '{print $1}'`
fi
case $SOCKPERF_PROTOCOL in
tcp)
	PROTOCOL_SWITCH=--tcp
	;;
udp)
	PROTOCOL_SWITCH=
	;;
*)
	die Unrecognised protocol, specify tcp or udp
	;;
esac

case $SOCKPERF_TESTTYPE in
ping-pong)
	SOCKPERF_MESSAGE_RATES=max
	ITERATIONS=10
	;;
throughput)
	SOCKPERF_MESSAGE_RATES=max
	ITERATIONS=10
	;;
under-load)
	ITERATIONS=1
	;;
esac

cd $SHELLPACK_SOURCES/sockperf-${VERSION}-installed || die Failed to cd to sockperf directory

export REMOTE_SERVER_SCRIPT=$SCRIPTDIR/shellpacks/shellpack-bench-sockperf
case $SERVERSIDE_COMMAND in
start)
	echo Starting server $TASKSET_SERVER
	mmtests_activity sockperf-$SERVERSIDE_NAME
	monitor_pre_hook $LOGDIR_RESULTS sockperf-$SERVERSIDE_NAME
	SOCKPERFROOT=$SHELLPACK_SOURCES/sockperf-${VERSION}-installed 
	killall sockperf 2> /dev/null
	nohup $TASKSET_SERVER $SOCKPERFROOT/bin/sockperf server $PROTOCOL_SWITCH 0<&- &>/dev/null &
	echo $! > /tmp/sockperf-server.pid
	sleep 2
	ps -p `cat /tmp/sockperf-server.pid`
	if [ $? -ne 0 ]; then
		die Failed to start sockperf
	fi
	exit $SHELLPACK_SUCCESS
	;;
stop)
	monitor_post_hook $LOGDIR_RESULTS sockperf-$SERVERSIDE_NAME
	echo Killing server
	kill `cat /tmp/sockperf-server.pid`
	rm /tmp/sockperf-server.pid
	exit $SHELLPACK_SUCCESS
	;;
flush)
	exit $SHELLPACK_SUCCESS
	;;
none)
	if [ -e /tmp/sockperf-server.pid ]; then
		echo Killing old servers
		kill `cat /tmp/sockperf-server.pid`
	fi

	echo Starting server
	SOCKPERFROOT=$SHELLPACK_SOURCES/sockperf-${VERSION}-installed 
	$TASKSET_SERVER $SOCKPERFROOT/bin/sockperf server $PROTOCOL_SWITCH &
	echo $! > /tmp/sockperf-server.pid
	sleep 2
	ps -p `cat /tmp/sockperf-server.pid`
	if [ $? -ne 0 ]; then
		die Failed to start sockperf
	fi
	;;
*)
	die Unrecognised server-side command
	;;
esac

for SIZE in $SOCKPERF_MESSAGE_SIZES; do
	for RATE in $SOCKPERF_MESSAGE_RATES; do
		mmtests_server_ctl start --serverside-name $SOCKPERF_PROTOCOL-$SIZE-$RATE
		mmtests_activity size $SOCKPERF_PROTOCOL-$SIZE-$RATE

		monitor_pre_hook $LOGDIR_RESULTS $SOCKPERF_PROTOCOL-$SIZE-$RATE
		###SHELLPACK iteration_begin $ITERATIONS
		echo Running $SOCKPERF_TESTTYPE test $PROTOCOL size $SIZE rate $RATE iteration $ITERATION/$ITERATIONS $TASKSET_CLIENT

		eval $TASKSET_CLIENT $SOCKPERFROOT/bin/sockperf 			\
			$SOCKPERF_TESTTYPE $PROTOCOL_SWITCH				\
			--full-log $LOGDIR_RESULTS/$SOCKPERF_PROTOCOL-$SIZE-${RATE}-$ITERATION.log \
			-t $SOCKPERF_DURATION						\
			--mps $RATE							\
			--msg-size $SIZE						\
			--no-rdtsc 2>&1 | tee $LOGDIR_RESULTS/$SOCKPERF_PROTOCOL-$SIZE-$RATE-${ITERATION}.stdout
		###SHELLPACK iteration_end
		monitor_post_hook $LOGDIR_RESULTS $SOCKPERF_PROTOCOL-$SIZE-$RATE
		gzip $LOGDIR_RESULTS/$SOCKPERF_PROTOCOL-$SIZE-${RATE}-$ITERATION.log

		mmtests_server_ctl stop --serverside-name $SOCKPERF_PROTOCOL-$SIZE-$RATE
	done
done

kill `cat /tmp/sockperf-server.pid`
rm /tmp/sockperf-server.pid

exit $SHELLPACK_SUCCESS
